package net.maxwoods.nimble;


import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.win32.StdCallLibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.sun.jna.Pointer.NULL;
import static com.sun.jna.platform.win32.WinBase.DWORD;
import static com.sun.jna.platform.win32.WinBase.INVALID_HANDLE_VALUE;
import static com.sun.jna.platform.win32.WinError.ERROR_SUCCESS;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;


public class NativeWindows{

    private static final Logger logger= LoggerFactory.getLogger(NativeWindows.class);

    public static void main(String[] args) throws Exception
    {
        Kernel32 kernel32 = Kernel32.INSTANCE;
        BlueTools blueTools=BlueTools.INSTANCE;
        BlueTools.BLUETOOTH_FIND_RADIO_PARAMS.ByReference bluetooth_find_radio_params=new BlueTools.BLUETOOTH_FIND_RADIO_PARAMS.ByReference();
        bluetooth_find_radio_params.dwSize=new DWORD (bluetooth_find_radio_params.size());
        WinNT.HANDLEByReference phRadio=new WinNT.HANDLEByReference();
        BlueTools.HBLUETOOTH_RADIO_FIND result=blueTools.BluetoothFindFirstRadio(bluetooth_find_radio_params,phRadio);
        logger.info("{},{}",result,phRadio.getValue());
        if(result==INVALID_HANDLE_VALUE)
        {
            logger.error("invlid handle");
        }
        boolean find=true;
        while (find) {
            BlueTools.BLUETOOTH_RADIO_INFO bluetooth_radio_info = new BlueTools.BLUETOOTH_RADIO_INFO();
            bluetooth_radio_info.dwSize = new DWORD(bluetooth_radio_info.size());
            DWORD ret = blueTools.BluetoothGetRadioInfo(phRadio.getValue(), bluetooth_radio_info);
            if ( !ret.equals( new DWORD(ERROR_SUCCESS) ) ) {
                logger.error("get bluetooth info error");
                continue;
            }
            logger.info("蓝牙无线电名称:{}",new String(bluetooth_radio_info.szName));
            BlueTools.BLUETOOTH_DEVICE_SEARCH_PARAMS.ByReference bluetoothDeviceSearchParams=new BlueTools.BLUETOOTH_DEVICE_SEARCH_PARAMS.ByReference();
            bluetoothDeviceSearchParams.dwSize=new DWORD(bluetoothDeviceSearchParams.size());
            bluetoothDeviceSearchParams.hRadio = phRadio.getValue();
            bluetoothDeviceSearchParams.fReturnAuthenticated = new WinDef.BOOL(TRUE);
            bluetoothDeviceSearchParams.fReturnConnected = new WinDef.BOOL(FALSE);
            bluetoothDeviceSearchParams.fReturnRemembered = new WinDef.BOOL(TRUE);
            bluetoothDeviceSearchParams.fReturnUnknown = new WinDef.BOOL(TRUE);
            bluetoothDeviceSearchParams.cTimeoutMultiplier = new WinDef.UCHAR(30);
            BlueTools.BLUETOOTH_DEVICE_INFO.ByReference bluetoothDeviceInfo=new BlueTools.BLUETOOTH_DEVICE_INFO.ByReference();
            bluetoothDeviceInfo.dwSize=new DWORD(bluetoothDeviceInfo.size());
            BlueTools.HBLUETOOTH_DEVICE_FIND hbluetoothDeviceFind=blueTools.BluetoothFindFirstDevice(bluetoothDeviceSearchParams,bluetoothDeviceInfo);
            int errCode=kernel32.GetLastError();
            logger.error("error code:{}",errCode);
            boolean bfind = hbluetoothDeviceFind != null;
            while(bfind)
            {

            }
            find=blueTools.BluetoothFindNextRadio(result,phRadio).booleanValue();
        }
        int errCode=kernel32.GetLastError();
        logger.error("error code:{}",errCode);
    }
}